package com.eascs.spider.yuqing.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

import com.eascs.spider.yuqing.common.proj.Task;

public class ObjectUtil {

	/**
	 * 字节转换成对象
	 * @param bytes
	 * @return
	 */
	public static Object ByteToObject(byte[] bytes) {  
		Object obj = null;  
		try {  
		    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);  
		    ObjectInputStream oi = new ObjectInputStream(bi);  
		    obj = oi.readObject();  
		    bi.close();  
		    oi.close();  
		} catch (Exception e) {  
		    e.printStackTrace();  
		}  
	return obj;  
	}  
	
	/**
	 * 对象转换字节
	 * @param obj
	 * @return
	 */
	public static byte[] ObjectToByte(Object obj) {  
	    byte[] bytes = null;  
	    try {  
	        ByteArrayOutputStream bo = new ByteArrayOutputStream();  
	        ObjectOutputStream oo = new ObjectOutputStream(bo);  
	        oo.writeObject(obj);  
	        bytes = bo.toByteArray();  
	        bo.close();  
	        oo.close();  
	    } catch (Exception e) {  
	        e.printStackTrace();  
	    }  
	    return bytes;  
	} 
	
	
	public static byte[] serialize(Object object) {  
        if (object == null) {  
            return null;  
        }  
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);  
        try {  
            ObjectOutputStream oos = new ObjectOutputStream(baos);  
            oos.writeObject(object);  
            oos.flush();  
        }  
        catch (IOException ex) {  
            throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);  
        }  
        return baos.toByteArray();  
    }
	
	 public static Object deserialize(byte[] bytes) {  
        if (bytes == null) {  
            return null;  
        }  
        try {  
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));  
            return ois.readObject();  
        }  
        catch (IOException ex) {  
            throw new IllegalArgumentException("Failed to deserialize object", ex);  
        }  
        catch (ClassNotFoundException ex) {  
            throw new IllegalStateException("Failed to deserialize object type", ex);  
        }  
    } 
	 
	/*public static <T> Map<String, String> toStringMap(T value) {
		Map<String, Object> valueMap = ObjectUtil.toMap(value);
		Map<String, String> stringMap = new HashMap<>();
		for (Map.Entry<String, Object> item : valueMap.entrySet()) {
			if (null == item.getValue())
				continue;
			stringMap.put(item.getKey(), item.getValue().toString());
		}
		return stringMap;
	}*/

	 public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
        if (map == null)  
            return null;    
        Object obj = beanClass.newInstance();  
        Field[] fields = obj.getClass().getDeclaredFields();   
        for (Field field : fields) {    
            int mod = field.getModifiers();    
            if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){    
                continue;    
            }    
            field.setAccessible(true);    
            field.set(obj, map.get(field.getName()));   
        }   
        return obj;    
    }    
  
    public static Map<String, String> objectToMap(Object obj) throws Exception {    
        if(obj == null){    
            return null;    
        }   
        Map<String, String> map = new HashMap<String, String>();    
  
        Field[] declaredFields = obj.getClass().getDeclaredFields();    
        for (Field field : declaredFields) {    
            field.setAccessible(true);  
            if(field.get(obj) instanceof String && field.get(obj) !=null){
            	map.put(field.getName(), (String)field.get(obj));
            }
        }    
        return map;  
    }   
	 
	 public static void main(String[] args) throws Exception {
		Task task = new Task();
		task.setUrl("www.baidu.com");
		System.out.println(objectToMap(task).toString());
	}
}
